Skip to content

ed448-goldilocks: apply final conditional swap in Montgomery ladder - #1996

Open
lyn1dev wants to merge 1 commit into
RustCrypto:masterfrom
lyn1dev:fix-ed448-montgomery-ladder
Open

lyn1dev wants to merge 1 commit into
RustCrypto:masterfrom
lyn1dev:fix-ed448-montgomery-ladder

Conversation

@lyn1dev

@lyn1dev lyn1dev commented Nov 24, 2026

Copy link
Copy Markdown

Summary

RFC 7748 §5 specifies that the Montgomery ladder concludes with a conditional swap based on the final swap value:
(x_2, x_3) = cswap(swap, x_2, x_3)\ and (z_2, z_3) = cswap(swap, z_2, z_3).

In \ed448-goldilocks, the scalar multiplication ladder in \src/montgomery.rs\ uses the lazy swap formulation (\choice = swap ^ bit, \swap = bit). For odd scalars, bit 0 is 1, so \swap\ is 1 upon exiting the loop. Without a final conditional swap, \x0\ and \x1\ remain swapped, causing &MontgomeryPoint * &EdwardsScalar\ to return [k + 1]P\ instead of [k]P.

Closes #1988

Changes

  • In \ed448-goldilocks/src/montgomery.rs, applied \ProjectiveMontgomeryPoint::conditional_swap(&mut x0, &mut x1, Choice::from(swap))\ after the bit loop before projecting to affine coordinates.
  • Added regression test \ est_montgomery_ladder_odd_scalars\ checking scalar multiplication against Edwards representation across both odd and even scalars (\k = 1..=16).

Verification

  • Reproduction confirmed: Without the final swap, \ est_montgomery_ladder_odd_scalars\ fails at \k = 1, where [1]G\ returned [2]G.
  • With the fix applied, all unit tests and doctests pass:
    • \cargo test -p ed448-goldilocks\ (68 passed, 0 failed)
    • \cargo test -p x448\ (3 passed, 1 ignored)
  • Linting and formatting:
    • \cargo clippy -p ed448-goldilocks --all-targets\ (0 warnings)
    • \cargo fmt -p ed448-goldilocks -- --check\ (clean)

AI was used to write this code.

RFC 7748 Section 5 specifies that the Montgomery ladder concludes with
a conditional swap based on the final swap value:
(x_2, x_3) = cswap(swap, x_2, x_3) and (z_2, z_3) = cswap(swap, z_2, z_3).

In ed448-goldilocks, the ladder loop uses the lazy swap form (choice = swap ^ bit;
swap = bit). For odd scalars, bit 0 is 1 so swap is 1 at loop termination,
leaving x0 and x1 swapped and returning [k+1]P instead of [k]P.

Applying conditional_swap with Choice::from(swap) restores x0 to [k]P for all
scalars.

Closes RustCrypto#1988

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ed448-goldilocks: MontgomeryPoint * Scalar returns [k+1]P for odd k (Montgomery ladder is missing the final conditional swap)

1 participant